/*
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that Software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of
 * any nuclear facility.
 */
package com.sun.j2ee.blueprints.supplierpo.ejb;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.RemoveException;

import com.sun.j2ee.blueprints.contactinfo.ejb.ContactInfoLocal;
import com.sun.j2ee.blueprints.contactinfo.ejb.ContactInfoLocalHome;
import com.sun.j2ee.blueprints.lineitem.ejb.LineItem;
import com.sun.j2ee.blueprints.lineitem.ejb.LineItemLocal;
import com.sun.j2ee.blueprints.lineitem.ejb.LineItemLocalHome;
import com.sun.j2ee.blueprints.servicelocator.ServiceLocatorException;
import com.sun.j2ee.blueprints.servicelocator.ejb.ServiceLocator;

/**
 * This is the main Entity Bean class for SupplierOrderEJB It has a one-many
 * relatioship with the LineItemEJB, and one-to-one relationship between
 * ContactInfoEJB
 */

public abstract class SupplierOrderEJB implements EntityBean {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2884516985036375076L;

	/**
	 * Accessor method for purchase order ID
	 * 
	 * @return String the PO id
	 */
	public abstract String getPoId();

	/**
	 * Setter method for PO id
	 * 
	 * @param String
	 *            the PO ID
	 */
	public abstract void setPoId(String id);

	/**
	 * Accessor method for purchase order Date
	 * 
	 * @return long the PO Date - time from epoch
	 */
	public abstract long getPoDate();

	/**
	 * Setter method for PO Date
	 * 
	 * @param long the PO Date - time from epoch
	 */
	public abstract void setPoDate(long orderDate);

	/**
	 * Accessor method for purchase order status
	 * 
	 * @return String the PO status
	 */
	public abstract String getPoStatus();

	/**
	 * Setter method for PO status
	 * 
	 * @param String
	 *            the PO status
	 */
	public abstract void setPoStatus(String stat);

	/**
	 * Accessor method for purchase order contact info
	 * 
	 * @return <Code>ContactInfoLocal</Code> the PO contactinfo
	 */
	public abstract ContactInfoLocal getContactInfo();

	/**
	 * Setter method for PO contact info
	 * 
	 * @param <Code>ContactInfoLocal</Code> the PO contact info
	 */
	public abstract void setContactInfo(ContactInfoLocal addr);

	/**
	 * Accessor method for purchase order line items
	 * 
	 * @return <Code>Collection</Code> the PO lientems
	 */
	public abstract Collection<LineItemLocal> getLineItems();

	/**
	 * Setter method for PO line items
	 * 
	 * @param <Code>Collection</Code> the PO lineitems
	 */
	public abstract void setLineItems(Collection litems);

	/**
	 * Method adds a line item to existing lineitems
	 * 
	 * @param <Code>LineItemLocal</Code> the line item details
	 */
	public void addLineItem(LineItemLocal lItem) {
		getLineItems().add(lItem);
	}

	/**
	 * the ejbCreate method
	 * 
	 * @param <code>supplierOrder</code>
	 */
	public String ejbCreate(SupplierOrder supplierOrder) throws CreateException {
		setPoId(supplierOrder.getOrderId());
		setPoDate(supplierOrder.getOrderDate().getTime());
		setPoStatus(OrderStatusNames.PENDING);
		return null;
	}

	/**
	 * the ejbPostCreate method
	 * 
	 * @param <code>supplierOrder</code>
	 */
	public void ejbPostCreate(SupplierOrder supplierOrder)
			throws CreateException {
		try {
			ServiceLocator serviceLocator = new ServiceLocator();
			ContactInfoLocalHome cinforef = (ContactInfoLocalHome) serviceLocator
					.getLocalHome(JNDINames.CINFO_EJB);
			ContactInfoLocal cinfoloc = cinforef.create(supplierOrder
					.getShippingInfo());
			setContactInfo(cinfoloc);
			LineItemLocalHome lineItemref = (LineItemLocalHome) serviceLocator
					.getLocalHome(JNDINames.LI_EJB);
			Collection<LineItem> litems = supplierOrder.getLineItems();
			Iterator<LineItem> it = litems.iterator();
			while ((it != null) && (it.hasNext())) {
				LineItem li = it.next();
				LineItemLocal lineItemloc = lineItemref.create(li, 0);
				addLineItem(lineItemloc);
			}
		} catch (ServiceLocatorException ne) {
			throw new CreateException(
					"ServiceLocator Ex while persisting PO CMR :"
							+ ne.getMessage());
		}
	}

	/**
	 * Method gets a collection of all line items for this PO. This is required
	 * because web tier cannot access CMR fields that return managed objects
	 * without a transaction
	 * 
	 * @return <Code>Collection</Code> Colleciton of lineitems
	 */
	public Collection<LineItem> getAllItems() {
		Collection<LineItemLocal> liColl = getLineItems();
		if (liColl == null)
			return (null);
		ArrayList<LineItem> retVal = new ArrayList<LineItem>();
		Iterator<LineItemLocal> it = liColl.iterator();
		while ((it != null) && (it.hasNext())) {
			LineItemLocal loc = it.next();
			retVal.add(loc.getData());
		}
		return (retVal);
	}

	public SupplierOrder getData() {
		SupplierOrder supplierOrder = new SupplierOrder();
		supplierOrder.setOrderId(getPoId());
		supplierOrder.setOrderDate(new Date(getPoDate()));
		supplierOrder.setShippingInfo(supplierOrder.getShippingInfo());
		Collection<LineItemLocal> lineItems = getLineItems();
		for (Iterator<LineItemLocal> iterator = lineItems.iterator(); iterator.hasNext();) {
			LineItemLocal lineItem = iterator.next();
			supplierOrder.addLineItem(lineItem.getData());
		}
		return supplierOrder;
	}

	// other ejb life cycle methods
	public void setEntityContext(EntityContext c) {
	}

	public void unsetEntityContext() {
	}

	public void ejbRemove() throws RemoveException {
	}

	public void ejbActivate() {
	}

	public void ejbPassivate() {
	}

	public void ejbStore() {
	}

	public void ejbLoad() {
	}
}
